package com.pjmike.netty.server.handler;

import com.pjmike.netty.protocol.message.packet.request.HeartbeatRequestPacket;
import com.pjmike.netty.protocol.message.packet.response.HeartbeatResponsePacket;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.Objects;

/**
 *
 */
@Slf4j
@ChannelHandler.Sharable
public class HeartBeatHandler extends SimpleChannelInboundHandler<HeartbeatRequestPacket> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HeartbeatRequestPacket msg) throws Exception {
        log.info("获取到客户端发来的心跳报文，报文体：{}", msg);
        if (Objects.nonNull(msg)) {
            HeartbeatResponsePacket heartbeatResponsePacket = new HeartbeatResponsePacket();
            BeanUtils.copyProperties(msg, heartbeatResponsePacket);
            ctx.writeAndFlush(heartbeatResponsePacket);
        }
    }
}
